package com.kefu.call.controller;

import cn.hutool.core.util.StrUtil;
import com.kefu.call.service.CallConnectService;
import com.kefu.call.vo.CallConnectParam.*;
import com.kefu.call.vo.CallNumberSearchVo;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.exception.BusinessException;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.crm.event.CrmCustomerEvent;
import com.kefu.crm.event.CrmCustomerLogEvent;
import com.kefu.crm.model.CrmCustomer;
import com.kefu.crm.utils.PhoneDat;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Mr Y
 * @Date: 2021/12/02/14:55
 * @Description:
 */
@Api(tags = "呼叫相关业务")
@RestController
@RequestMapping("call/connect/")
public class CallConnectController extends AbstractBaseController {
    @Autowired
    PhoneDat phoneDat;
    @Resource
    CallConnectService callConnectService;

    @PostMapping("getCall")
    @ApiOperation(value = "获取通话业务数据", notes = "获取通话业务数据")
    public Result<CallGetR> getCall(@Validated @RequestBody CallGetP param) {

        CallGetR result = callConnectService.getCall(param);
        return ResultGenerator.genSuccessResult(result);
    }

    @PostMapping("callHistoryList")
    @ApiOperation(value = "呼叫历史纪录", notes = "呼叫历史纪录")
    public Result<List<CallHistoryR>> callHistoryList(@Validated @RequestBody CallHistoryP param) {
        if(StringUtils.isAllBlank(param.getPhone(),param.getTaskDetailId())){
            return ResultGenerator.genSuccessResult();
        }
        return ResultGenerator.genSuccessResult(callConnectService.callHistoryList(param));
    }

    @PostMapping("blackUser")
    @ApiOperation(value = "呼叫拉黑客户", notes = "呼叫拉黑客户")
    public Result<String> blackUser(@Validated @RequestBody BlackUserP param) {
        logger.info("呼叫拉黑客户:{}", param);
        CrmCustomer crmCustomer = callConnectService.blackUser(param);
        publisher.publishEvent(new CrmCustomerLogEvent(crmCustomer, 0 == param.getBlackMode() ? 4 : 5));
        publisher.publishEvent(new CrmCustomerEvent(crmCustomer));
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("callDetail")
    @ApiOperation(value = "查询呼叫详情", notes = "查询呼叫详情")
    public Result<CallDetailR> callDetail(@Validated @RequestBody CallDetailP param) {
        if (StrUtil.hasBlank(param.getCallId(), param.getCallType())) {
            throw BusinessException.msg("查询失败");
        }
        return ResultGenerator.genSuccessResult(callConnectService.callDetail(param));
    }


    @PostMapping("makeFailCall")
    @ApiOperation(value = "呼入标记为失败", notes = "呼入标记为失败")
    public Result<String> makeFailCall(@Validated @RequestBody String callId) {
        logger.info("呼入标记为失败:{}", callId);
        return ResultGenerator.genSuccessResult(callConnectService.makeFailCall(callId));
    }
    
    @PostMapping("getCallNumberCity")
    @ApiOperation(value = "获取呼叫归属地", notes = "获取呼叫归属地")
    public Result<String> getCallNumberCity(@Validated @RequestBody CallNumberSearchVo searchVo) {
    	logger.info("获取呼叫归属地:{}", searchVo);
    	return ResultGenerator.genSuccessResult(phoneDat.getCity(searchVo.getPhone()));
    }
}
